package com.sinopec.handdevice.view;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.gyf.immersionbar.ImmersionBar;
import com.sinopec.basemodule.AppConstants;
import com.sinopec.basemodule.base.BaseActivity;
import com.sinopec.basemodule.base.CustomToolbar;
import com.sinopec.basemodule.utils.ARouterManagerUtils;
import com.sinopec.basemodule.utils.CommonUtils;
import com.sinopec.handdevice.R;
import com.sinopec.handdevice.contract.MainContract;
import com.sinopec.handdevice.presenter.MainPresenter;
import com.sinopec.resmodule.entity.TabEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lwj
 * @ClassName: MainActivity
 * @Description: 主界面
 * @date 2019/4/3 19:20
 */
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private CommonTabLayout commonTab;
    private double mLastClickTime;
    private ArrayList<CustomTabEntity> mTabEntities;
    private ArrayList<Fragment> mFragments;
    private List<String> mTitles;
    private List<Integer> mIconUnselectedIds;
    private List<Integer> mIconSelectIds;

    private int index = 0;

    @SuppressWarnings("ConstantConditions")
    @Override
    protected void initView(Bundle savedInstanceState) {
        List<String> titles = Arrays.asList("入库", "出库", "出库管理");
        List<Integer> iconUnselectedIds = Arrays.asList(
                R.drawable.icon_stock_in_normal,
                R.drawable.icon_stock_out_normal,
                R.drawable.icon_mine_normal
        );
        List<Integer> iconSelectIds = Arrays.asList(
                R.drawable.icon_stock_in_selected,
                R.drawable.icon_stock_out_selected,
                R.drawable.icon_mine_selected
        );
        mTitles = new ArrayList<>(titles);
        mIconSelectIds = new ArrayList<>(iconSelectIds);
        mIconUnselectedIds = new ArrayList<>(iconUnselectedIds);
        index = index >= mTitles.size() ? mTitles.size() - 1 : index; //防止数组溢出
        mTabEntities = new ArrayList<>();
        mFragments = new ArrayList<>();
        commonTab = findViewById(R.id.ctl_bottom);

        for (int i = 0; i < mTitles.size(); i++) {
            mTabEntities.add(new TabEntity(mTitles.get(i), mIconSelectIds.get(i), mIconUnselectedIds.get(i)));
        }

        //入库
        Fragment fragmentStockIn = ARouterManagerUtils.gotoStockInFragment();
        //出库
        Fragment fragmentStockOut = ARouterManagerUtils.gotoStockOutFragment();
        //我的
        Fragment fragmentInventory = ARouterManagerUtils.gotoMineFragment();
        //设置
        //Fragment setInventory = ARouterManagerUtils.gotoUhfSetScan();

        if (fragmentStockIn == null || fragmentStockOut == null || fragmentInventory == null) {
            CommonUtils.showToast(mContext, "业务组件单独调试不应该跟其他业务Module产生交互，如果你依然想要在运行期依赖其它组件，那么请参考MainModule");
        } else {
            mFragments.add(fragmentStockIn);
            mFragments.add(fragmentStockOut);
            mFragments.add(fragmentInventory);
            //mFragments.add(setInventory);
        }
        commonTab.setTabData(mTabEntities, this, R.id.main, mFragments);
        commonTab.setCurrentTab(index);
        commonTab.setIconHeight(40);
        commonTab.setIconWidth(40);
        commonTab.setIconMargin(0);
        commonTab.setTextsize(0);
        showTitle(index);
        commonTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                showTitle(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new MainPresenter(this);
    }

    @Override
    public int getContentView() {
        return R.layout.activty_main;
    }

    @Override
    public void configToolbar(CustomToolbar rToolbar) {
        rToolbar.setToolbarVisible(false);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        super.onNewIntent(intent);
        index = intent.getIntExtra(AppConstants.HOME_PAGE_INDEX, 0);
        commonTab.setCurrentTab(index);
        showTitle(index);
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - mLastClickTime > 1000) {
            Toast.makeText(this, "再次点击退出应用", Toast.LENGTH_SHORT).show();
            mLastClickTime = System.currentTimeMillis();
        } else {
            MainActivity.this.finish();
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.clear();
        outState.putInt("saveCurrentIndex", commonTab.getCurrentTab());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            int index = savedInstanceState.getInt("saveCurrentIndex", 0);
            commonTab.setCurrentTab(index);
            showTitle(index);
        }
    }

    private void showTitle(int position) {
        switch (position) {
            case 0:
                mCustomToolbar.setTitle("入库");
                ImmersionBar.with(this).statusBarColor("#F0F0F0").statusBarDarkFont(true).init();
                break;
            case 1:
                mCustomToolbar.setTitle("出库");
                ImmersionBar.with(this).statusBarColor("#F0F0F0").statusBarDarkFont(true).init();
                break;
            case 2:
                mCustomToolbar.setTitle("库存管理");
                ImmersionBar.with(this).statusBarColor("#FFFFFF").statusBarDarkFont(true).init();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
